import { createRouter, createWebHashHistory } from "vue-router";

const routes = [
  {
    path: "/",
    redirect: "/home",
  },
  {
    path: "/home",
    component: () => import("../views/Home.vue"),
    children: [
      {
        path: "/home",
        redirect: "/home/index",
      },
      {
        path: "/home/index",
        component: () => import("../views/home/Home.vue"),
      },
      {
        path: "/home/community",
        component: () => import("../views/home/Community.vue"),
      },
      {
        path: "/home/my",
        component: () => import("../views/home/My.vue"),
      },
      {
        path: "/home/news",
        component: () => import("../views/home/News.vue"),
      },
      {
        path: "/home/trip",
        component: () => import("../views/home/Trip.vue"),
      },
    ],
  },
  {
    path: "/busticket",
    component: () => import("../views/BusTicket.vue"),
  },
  {
    path: "/vipcenter",
    component: () => import("../views/Vipcenter.vue"),
    meta: {
      requireLogin: true,
    },
  },
  {
    path: "/city",
    component: () => import("../views/City.vue"),
  },
  {
    path: "/address",
    component: () => import("../views/Address.vue"),
  },
  {
    path: "/Addaddress",
    component: () => import("../views/Addaddress.vue"),
  },
  {
    path: "/login",
    component: () => import("../views/Login.vue"),
  },
  {
    path: "/live",
    component: () => import("../views/Live.vue"),
  },
  {
    path: "/planeticket",
    component: () => import("../views/PlaneTicket.vue"),
  },
  {
    path: "/renttaking",
    component: () => import("../views/RentTaking.vue"),
  },
  {
    path: "/train",
    component: () => import("../views/Train.vue"),
  },
  {
    path: "/travel",
    component: () => import("../views/Travel.vue"),
  },
  {
    path: "/camera",
    component: () => import("../views/Camera.vue"),
  },
  {
    path: "/:catchAll(.*)",
    component: () => import("../views/404.vue"),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});
router.beforeEach((to, from, next) => {
  if (to.meta.requireLogin) {
    // 说明这个页面需要登录
    if (localStorage.getItem("token")) {
      // 说明已经登录
      next();
    } else {
      next("/login");
    }
  } else {
    next();
  }
});

export default router;
